package com.easy.core.util;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.easy.base.model.Operator;
import com.easy.core.param.GlobalConst;

/**
 * 描述：servlet工具类
 * @package com.easy.core.util
 * @class ServletUtils
 */
public class ServletUtil {
	
	public static HttpServletRequest getRequest() {
//		if(null == ActionContext.getContext()) { return null; }
//		return ServletActionContext.getRequest();
		return null;
	}
	
	public static HttpSession getSession() {
		HttpServletRequest request = getRequest();
		if(null == request) { return null; }
		return request.getSession();
	}
	
	public static String getIpAddr() {
		return getIpAddr(getRequest());
	}
	
	//获取客户端IP地址
	public static String getIpAddr(HttpServletRequest request) {
		String ip = request.getHeader("x-forwarded-for");
		if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("Proxy-Client-IP");
		}
		if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}
		return ip;
	}
	
	//获取当前登录人
	public static Operator getCurrentLoginUser() {
		HttpSession session  = getSession();
		if(null == session) { return null; }
		Object obj = session.getAttribute(GlobalConst.HTTP_CURRENT_OPERATOR_KEY);
		if( null == obj || !(obj instanceof Operator)) { return null; }
		return (Operator)obj;
	}
}
